package com.example.yuntiku.dao.impl;

import com.example.yuntiku.dao.BaseDao;
import com.example.yuntiku.dao.UserDao;
import com.example.yuntiku.entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public void registerUser(User user) {
        // 在数据库中插入用户记录的具体逻辑
        String sql="INSERT INTO users (uname, upwd, email) VALUES (?, ?, ?)";
        super.executeUpdate(sql,
                user.getUname(),
                user.getUpwd(),
                user.getEmail());
    }

    @Override
    public User getUserByEmail(String email) {
        // 根据邮箱从数据库中查询并返回对应的用户
        User user=new User();
        String sql="SELECT `uid`,`uname`,`upwd`,`email`,`cid`,`houseid` FROM users WHERE email = ?";
        ResultSet rs=null;
        try {
            rs=super.executeQuery(sql);
            while (rs.next()) { //一次读取一行记录，读完返回false
                //转换成实体类对象，一个对象，装一条数据
                User entity=new User();
                entity.setUid(rs.getInt("uid"));
                entity.setUname(rs.getString("uname"));
                entity.setUpwd(rs.getString("upwd"));
                entity.setEmail(rs.getString("email"));
                entity.setCid(rs.getInt("cid"));
                entity.setHouseid(rs.getInt("houseid"));
                return entity;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            super.closeAll(conn,null,rs);
        }
        return user;
    }

    @Override
    public boolean verifyEmailCode(String email, String code) {
        // 验证邮箱验证码的具体逻辑，例如与发送的验证码进行比较
        // 这里可以根据你的功能需求自定义实现方式
        // 可以与用户输入的验证码进行比较，验证是否匹配
        return false;
    }

    @Override
    public List<User> selectByClassId(String[] classIds) {
        List<User> list = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        String sql="SELECT `uid`,`uname`,`upwd`,`email`,`cid`,`houseid`\n" +
                "FROM `users`\n" +
                "WHERE `cid` IN(";
        for (int i = 0; i < classIds.length; i++) {
            sql+="?,";
            params.add(classIds[i]);
        }
        sql=sql.substring(0,sql.length()-1);//去掉最后一个逗号
                sql+=")";
        ResultSet rs=null;
        try {
            rs=super.executeQuery(sql,params.toArray());
            while (rs.next()) { //一次读取一行记录，读完返回false
                //转换成实体类对象，一个对象，装一条数据
                User entity=new User();
                entity.setUid(rs.getInt("uid"));
                entity.setUname(rs.getString("uname"));
                entity.setUpwd(rs.getString("upwd"));
                entity.setEmail(rs.getString("email"));
                entity.setCid(rs.getInt("cid"));
                entity.setHouseid(rs.getInt("houseid"));
                list.add(entity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            super.closeAll(conn,null,rs);
        }
        return list;
    }
}
